﻿using DevExpress.Utils;
using DevExpress.XtraEditors.Calendar;
using DevExpress.XtraGrid.Views.Base;
using Extensions;
using OTS.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;

namespace OTS
{
    public partial class Ajanda : XForm
    {
        DateTime SeciliGun;
        Dictionary<DateTime, decimal> dcGunlereGoreToplamTutarlar = new Dictionary<DateTime, decimal>();
        Dictionary<DateTime, string> dcTatilGunleri = new Dictionary<DateTime, string>();

        List<DateTime> lsTatiller = new List<DateTime>();

        public Ajanda()
        {
            InitializeComponent();
        }

        private void Ajanda_Load(object sender, EventArgs e)
        {
            SeciliGun = DateTime.Now;
            DataTable dt = AppSession.DataManager.ExecuteDataTable("GetToplamOdeme");
            foreach (DataRow dr in dt.Rows)
                dcGunlereGoreToplamTutarlar.Add((DateTime)dr[0], (decimal)dr[1]);

            dt = AppSession.DataManager.ExecuteDataTable("GetTatilGunleri");
            foreach (DataRow dr in dt.Rows)
            {
                if (!dcTatilGunleri.ContainsKey((DateTime)dr["Tarih"]))
                {
                    dcTatilGunleri.Add((DateTime)dr["Tarih"], (string)dr["TatilGunu"]);
                }
                lsTatiller.Add((DateTime)dr["Tarih"]);
            }
            InitDetay();
            DetayGuncelle();
        }

        Color clr1, clr2;
        double Rx, Gx, Bx, Max;

        public Color getColor(int tutar)
        {
            if (tutar == 0)
                return Color.White;
            else if (tutar > Max)
                return clr2;
            else
                return Color.FromArgb(
                    clr1.R + (int)(tutar * Rx),
                    clr1.G + (int)(tutar * Gx),
                    clr1.B + (int)(tutar * Bx));
        }

        private void InitDetay()
        {
            Max = 2e6;
            //clr1 = Color.FromArgb(255, 255, 255);
            //clr2 = Color.FromArgb(255, 100, 100);

            clr1 = Color.FromArgb(255, 255, 255);
            clr2 = Color.FromArgb(255, 100, 100);

            Rx = (clr2.R - clr1.R) / Max;
            Gx = (clr2.G - clr1.G) / Max;
            Bx = (clr2.B - clr1.B) / Max;

            gvDetail.ColumnPanelRowHeight = 32;
            gvDetail.Appearance.HeaderPanel.TextOptions.WordWrap = WordWrap.Wrap;
            gvDetail.OptionsView.EnableAppearanceEvenRow = true;
            gvDetail.CustomDrawCell += gvDetail_CustomDrawCell;

            DataSet dsData = AppSession.DataManager.ExecuteDataSet("GetOdemeTablosu", AppSession.UserInfo.SirketId, SeciliGun, SeciliGun);
            gvDetail.Init(dsData.Tables[1]);
            //   DataRow dr=new DataRow();
            //   DataColumn dc = new DataColumn("OdemeId",typeof(string));
            //   DataColumn dc1 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc2 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc3 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc4 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc5 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc6 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc7 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc8 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc9 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc10 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc11 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc12 = new DataColumn("OdemeId", typeof(string));
            //   DataColumn dc13 = new DataColumn("OdemeId", typeof(string));
            //   dr[dc].
            ////   dsData.Tables[1].Rows.Add(
            gvDetail.AddSummary();
            gvDetail.FormatAsNumber(2, "Miktar", "IslemTarihindekiKur", "IslemTarihindekiTL", "ReelKur", "ReelTL");
            gvDetail.MakeColumnInvisible("SirketId", "HareketTipiId");
            gvDetail.SetCaptions("OdemeId:#", "Aciklama:Açıklama", "IslemTarihindekiKur:Giriş Kur", "IslemTarihindekiTL:Giriş TL", "Parabirimi:PB");
            gvDetail.FormatAsDate("Tarih");
            gvDetail.AddSummary("IslemTarihindekiTL", "ReelTL");
        }

        private void DetayGuncelle()
        {

            if (dcTatilGunleri.ContainsKey(SeciliGun))
            {
                lblSeciliTarih.Text = SeciliGun.ToString("dd/MMMM/yyy");
                lblTatilAdi.Text = "(" + dcTatilGunleri.FirstOrDefault(x => x.Key == SeciliGun).Value + ")";
            }
            else
            {
                lblSeciliTarih.Text = SeciliGun.ToString("dd/MMMM/yyy");
                lblTatilAdi.Text = "";
            }



            DataSet dsData = AppSession.DataManager.ExecuteDataSet("GetOdemeTablosu", AppSession.UserInfo.SirketId, SeciliGun, SeciliGun);
            gvDetail.VeriyiGuncelle(dsData.Tables[1]);
            gvDetail.BestFitColumns();
            dnTarih.DateTime = SeciliGun.Date;
        }

        private void dnTarih_CustomDrawDayNumberCell(object sender, CustomDrawDayNumberCellEventArgs e)
        {
            int OdemeTutari = ((dcGunlereGoreToplamTutarlar.FirstOrDefault(x => x.Key == e.Date).Value) * (-1)).ToIntZero();

            e.Graphics.FillRectangle(new SolidBrush(getColor(OdemeTutari)), e.Bounds);

            if (e.Date.DayOfWeek == DayOfWeek.Saturday || e.Date.DayOfWeek == DayOfWeek.Sunday)
                e.Graphics.DrawString(e.Date.Day.ToString(), new Font(e.Style.Font.FontFamily, e.Style.Font.Size, FontStyle.Bold), Brushes.Black, e.Bounds);
            else
                if (lsTatiller.Contains(e.Date))
                    e.Graphics.DrawString(e.Date.Day.ToString(), new Font(e.Style.Font.FontFamily, e.Style.Font.Size, FontStyle.Bold), Brushes.Black, e.Bounds);
                else
                    e.Graphics.DrawString(e.Date.Day.ToString(), e.Style.Font, Brushes.Black, e.Bounds);

            e.Handled = true;
        }

        void gvDetail_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (new string[] { "Miktar", "IslemTarihindekiTL", "ReelTL" }.Contains(e.Column.FieldName) && e.CellValue.ToIntNN() < 0)
            {
                e.Appearance.ForeColor = Color.Red;
            }
        }

        private void dnTarih_Click(object sender, EventArgs e)
        {
            SeciliGun = dnTarih.Selection[0];
            DetayGuncelle();
        }
        private void btnBuGun_Click(object sender, EventArgs e)
        {
            SeciliGun = DateTime.Now;
            DetayGuncelle();
        }

        private void btnSonrakiGun_Click(object sender, EventArgs e)
        {
            SeciliGun = SeciliGun.AddDays(1);
            DetayGuncelle();
        }

        private void btnOncekiGun_Click(object sender, EventArgs e)
        {
            SeciliGun = SeciliGun.AddDays(-1);
            DetayGuncelle();
        }
    }
}